<?php
/**
 * 报表管理
 * @author jianhui
 *
 */
class Report extends MY_Controller{
    var $reportType;
    function Report(){
        parent::MY_Controller();
        $this->reportType = array(
            1 => '线上明细表',
            2 => '线下明细表',
            3 => '项目费用表',
            4 => '标准动作执行统计表',
        );
        $this->load->model('timeUnitModel');
    }
    /**
     * 报表管理首页
     */
    function index(){
        //查询条件
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $timeUnit = (int)$this->input->get('timeUnit');
        $timeUnit || $timeUnit = $this->timeUnitModel->getNowTimeUnit();
        $this->viewData['timeUnit'] = $timeUnit;
        //是否为补报
        $isBubao = (int)$this->input->get('isBubao');
        //报表类型
        $reportType = (int)$this->input->get('reportType');
        if (isset($_GET['submit']) && $_GET['submit'] == 'export'){
            //条件参数
            $params = array('area'=>$area, 'region'=>$region, 'province'=>$province, 'city'=>$city, 'storeId'=>$storeId, 'keyword'=>$keyword, 'year'=>$year, 'timeUnit'=>$timeUnit, 'isBubao'=>$isBubao);
            //导出文件名
            $fileName = $this->reportType[$reportType].'-'.$year.'年';
            if ($timeUnit){
                $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
                isset($timeUnitList[$year][$timeUnit]) && $fileName .= '-'.$timeUnitList[$year][$timeUnit];
            }
            $fileName .= '-'. ($isBubao == 1 ? '补报':'普通申报');
            
            //主线流程条件
            $procWhere = array();
            //查找对应地区列表
            $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
            $placeStoreIds = array_keys($placeStoreList);
            $this->viewData['allStoreList'] = $placeStoreList;
            //权限内专营店列表
            $viewStoreIds = $this->userLib->getViewStoreIds();
            if ($viewStoreIds == '*'){
                $procWhere['storeId'] = array('in', $placeStoreIds);
            }else{
                $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
            }
            $procWhere['year'] = $params['year'];
            $procWhere['timeUnit'] = $params['timeUnit'];
            $this->load->model('singleProcessModel');
            $procList = $this->singleProcessModel->getNewList($procWhere);
            $this->viewData['procList'] = $procList;
            //百县强基项目条件
            $projectWhere = array();
            $this->load->model('sProjectModel');
            $projectWhere['procId'] = array('in', array_keys($procList));
            $projectWhere['isBubao'] = $params['isBubao'];
            $projectList = $this->sProjectModel->getNewList($projectWhere);
            $this->viewData['projectList'] = $projectList;
            //设置文件下载头
            setExcelDownHeader($fileName);
            
            switch ($reportType){
                case 1://线上明细表
                    $this->getSingleGxExcel($params);
                    break;
                case 2://下下明细表
                    $this->getSingleOfflineExcel($params);
                    break;
                case 3://项目费用表
                    $this->getSingleProjectExcel($params);
                    break;
                case 4://标准动作执行统计表
                    $this->getSingleActionExcel($params);
                    break;
            }
        }else{
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('Report', 'index'),'title'=>'报表管理');
            
            $this->load->view('Report/index', $this->viewData);
        }
    }
    /**
     * 百县强基-线上明细表
     * @param array $params 筛选条件
     */
    function getSingleGxExcel($params){
        //广宣条件
        $gxWhere = array();
        $this->load->model('sPGxModel');
        $gxWhere['projectId'] = array('in', array_keys($this->viewData['projectList']));
        $gxList = $this->sPGxModel->getNewList($gxWhere, NULL, NULL, '', 'projectId ASC');
        $this->viewData['gxList'] = $gxList;
        $this->load->view('Report/singleGxExcel', $this->viewData);
    }
    /**
     * 百县强基-线下明细表
     * @param array $params 筛选条件
     */
    function getSingleOfflineExcel($params){
        //线下活动条件
        $offlineWhere = array();
        $this->load->model('sPOfflineModel');
        $offlineWhere['projectId'] = array('in', array_keys($this->viewData['projectList']));
        $offlineList = $this->sPOfflineModel->getNewList($offlineWhere, NULL, NULL, '', 'projectId ASC');
        $this->viewData['offlineList'] = $offlineList;
        //线下活动明细金额汇总列表
        $this->load->model('sPODetailModel');
        $moneyDetailList = $this->sPODetailModel->getMoneyDetailList($offlineList);
        $this->viewData['moneyDetailList'] = $moneyDetailList;
        
        $this->load->view('Report/singleOfflineExcel', $this->viewData);
    }
    /**
     * 百县强基-项目费用表
     * @param array $params 筛选条件
     */
    function getSingleProjectExcel($params){
        //百县强基项目费用明细列表
        $this->viewData['projectMoneyDetail'] = $this->sProjectModel->getMoneyDetailList($this->viewData['projectList']);
        
        $this->load->view('Report/singleProjectExcel', $this->viewData);
    }
    /**
     * 百县强基-标准动作执行统计表
     * @param array $params 筛选条件
     */
    function getSingleActionExcel($params){
        //标准动作数据
        $this->load->model('actionModel');
        $actionList = $this->actionModel->getActiveList($params['timeUnit'], 1, $params['year']);
        $this->viewData['actionList'] = $actionList;
        //标准动作数据分析
        $actionDetailList = $this->sProjectModel->explanAction($this->viewData['projectList'], $actionList);
        $this->viewData['actionDetailList'] = $actionDetailList;
        $this->load->view('Report/singleActionExcel', $this->viewData);
    }
}